package com.hutao.medical.service.impl;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hutao.medical.common.config.ConfigProperties;
import com.hutao.medical.common.util.HttpClientUtil;
import com.hutao.medical.common.util.JsonUtils;
import com.hutao.medical.common.util.Result;
import com.hutao.medical.constant.Constant;
import com.hutao.medical.dataSource.TargetDataSource;
import com.hutao.medical.mapper.medicalRecord.TreatmentTraceMapper;
import com.hutao.medical.mapper.retrieval.MedRecordRetrievalMapper;
import com.hutao.medical.service.MedRecordService;


@Transactional
@Service
public class MedRecordServiceImpl implements MedRecordService{

	@Autowired
	private ConfigProperties configProperties;
	
	@Autowired
	private MedRecordRetrievalMapper medRecordRetrievalMapper;
	
	@Autowired
	private TreatmentTraceMapper treatmentTraceMapper;

	
	@Override
	public Map<String, List<Map<String, Object>>> getBaseInfoMap() {
		Map<String,List<Map<String,Object>>> baseInfoMap=new HashMap<>();
		if(MapUtils.isNotEmpty(Constant.baseInfoMap)){
			baseInfoMap=Constant.baseInfoMap;
		}else{
			String userJson = HttpClientUtil.doPost(configProperties.getMedicalManage().getBasicDataPath());
			if(StringUtils.isNoneEmpty(userJson)){
				Result result=JsonUtils.jsonToPojo(userJson, Result.class);
				if(result.isSuccess()){
					baseInfoMap=(Map<String, List<Map<String, Object>>>) result.getData();
					Constant.baseInfoMap=baseInfoMap;
				}
			}
		}
		return baseInfoMap;
	}


	/**
	 * 入院统计
	 * 
	 */
	@Transactional(readOnly = true)
	@TargetDataSource(name="ds1")
	@Override
	public List<Map<String, Object>> admissionStatistics(String beginDate, String endDate) {
		return medRecordRetrievalMapper.admissionStatistics(beginDate, endDate);
	}

	/**
	 * 从事职业构成统计
	 * 
	 */
	@Transactional(readOnly = true)
	@TargetDataSource(name = "ds1")
	@Override
	public List<Map<String, Object>> makeACareerStatistics(String beginDate, String endDate) {
		return medRecordRetrievalMapper.makeACareerStatistics(beginDate, endDate);
	}

	/**
	 * 付费统计方式
	 * 
	 */
	@Transactional(readOnly = true)
	@TargetDataSource(name = "ds1")
	@Override
	public List<Map<String, Object>> wayOfPayingStatistics(String beginDate, String endDate) {
		return medRecordRetrievalMapper.wayOfPayingStatistics(beginDate, endDate);
	}

	/**
	 * 诊治疾病顺位
	 * 
	 */
	@Transactional(readOnly = true)
	@TargetDataSource(name = "ds1")
	@Override
	public List<Map<String, Object>> diagnosisAndTreatmentOfDiseases(Map<String, Object> params) {
		return medRecordRetrievalMapper.diagnosisAndTreatmentOfDiseases(params);
	}

	/**
	 * 住院手术顺位
	 * 
	 */
	@Transactional(readOnly = true)
	@TargetDataSource(name = "ds1")
	@Override
	public List<Map<String, Object>> hospitalizationInLineStatistics(Map<String, Object> params) {
		return medRecordRetrievalMapper.hospitalizationInLineStatistics(params);
	}

	@Transactional(readOnly = true)
	@TargetDataSource(name = "ds1")
	@Override
	public List<Map<String, Object>> inHospitalRouteExcelList(String beginDate, String endDate) {
		List<Map<String,Object>> linkMapList = new ArrayList<>();
		List<Map<String,Object>> list = medRecordRetrievalMapper.admissionStatistics(beginDate, endDate);
		Map<String,Object> linkedMap =null;
		double totalCount=0;
		
		if(CollectionUtils.isNotEmpty(list)){
			for (Map<String, Object> map : list) {
				totalCount+=map.get("num")==null?0:(long)map.get("num");
			}
			
			for (Map<String, Object> map : list) {
				double inHospitalNum=0;
				linkedMap = new LinkedHashMap<>();
				inHospitalNum=map.get("num")==null?0:(long)map.get("num");
				linkedMap.put("name", map.get("typeName")==null?"":map.get("typeName"));
				linkedMap.put("num", inHospitalNum);
				if(totalCount==0){
					linkedMap.put("rate", "0.00%");
				}else{
					linkedMap.put("rate", new DecimalFormat("#0.00").format(inHospitalNum/totalCount));
				}
				
				linkMapList.add(linkedMap);
				
			}
			
			return linkMapList;
			
		}
		return null;
	}


	@Transactional(readOnly = true)
	@TargetDataSource(name = "ds1")
	@Override
	public List<Map<String, Object>> occpationExcelList(String beginDate, String endDate) {
		List<Map<String,Object>> sourceList = medRecordRetrievalMapper.makeACareerStatistics(beginDate, endDate);
		List<Map<String,Object>> targetList = new ArrayList<>();
		double totalCount=0;
		Map<String,Object> targetMap =null;
 		if(CollectionUtils.isNotEmpty(sourceList)){
			for (Map<String, Object> map : sourceList) {
				totalCount+=map.get("num")==null?0:(long)map.get("num");
			}
			
			for (Map<String, Object> map : sourceList) {
				double jobNum=map.get("num")==null?0:(long)map.get("num");
				
				targetMap =new LinkedHashMap<>();
				targetMap.put("professionName", map.get("professionName")==null?"":(String) map.get("professionName"));
				targetMap.put("num", jobNum);
				if(totalCount==0){
					targetMap.put("rate","0.00");
				}else{
					targetMap.put("rate",  new DecimalFormat("#0.00").format(jobNum/totalCount));
				}
				targetList.add(targetMap);
			}
			return targetList;
		}
		return null;
	}


	@Transactional(readOnly = true)
	@TargetDataSource(name = "ds1")
	@Override
	public List<Map<String, Object>> paymentExcelList(String beginDate, String endDate) {
		List<Map<String,Object>> sourceList = medRecordRetrievalMapper.wayOfPayingStatistics(beginDate, endDate);
		List<Map<String,Object>> targetList = new ArrayList<>();
		double totalCount=0;
		Map<String,Object> targetMap =null;
		if(CollectionUtils.isNotEmpty(sourceList)){
			for (Map<String, Object> map : sourceList) {
				totalCount+=map.get("num")==null?0:(long)map.get("num");
			}
			
			for (Map<String, Object> map : sourceList) {
				double jobNum=map.get("num")==null?0:(long)map.get("num");
				
				targetMap =new LinkedHashMap<>();
				targetMap.put("payTypeName", map.get("payTypeName")==null?"":(String) map.get("payTypeName"));
				targetMap.put("num", jobNum);
				if(totalCount==0){
					targetMap.put("rate","0.00");
				}else{
					targetMap.put("rate",  new DecimalFormat("#0.00").format(jobNum/totalCount));
				}
				targetList.add(targetMap);
			}
			return targetList;
		}
		return null;
	}


	@Transactional(readOnly = true)
	@TargetDataSource(name = "ds1")
	@Override
	public List<Map<String, Object>> fillingWorkExcelList(String beginDate, String endDate) {
		Map<String,Object> map = new HashMap<>();
		map.put("beginDate", beginDate);
		map.put("endDate", endDate);
		List<Map<String,Object>> targetList = new ArrayList<>();
		List<Map<String,Object>> list =  treatmentTraceMapper.getWorkloadStatisticsDateOfFiling(map);
		Map<String,Object> targetMap = null;
		for (Map<String, Object> map2 : list) {
			targetMap = new LinkedHashMap<>();
			targetMap.put("userName", map2.get("userName")==null?"":(String)map2.get("userName"));
			targetMap.put("perosnalSum", map2.get("perosnalSum")==null?0:(long)map2.get("perosnalSum"));
			String rate="0";
			if( map2.get("personalPercent")!=null){
				BigDecimal rateDecimal =(BigDecimal) map2.get("personalPercent");
				rate=rateDecimal.toString();
			}
			targetMap.put("personalPercent", rate);
			targetList.add(targetMap);
		}
		
		
		return targetList;
	}

	@Transactional(readOnly = true)
	@TargetDataSource(name = "ds1")
	@Override
	public List<Map<String, Object>> qualityWorkExcelList(String beginDate, String endDate) {
		Map<String,Object> params = new HashMap<>();
		params.put("beginDate", beginDate);
		params.put("endDate", endDate);
		List<Map<String,Object>> targetList = new ArrayList<>();
		List<Map<String,Object>> list =  treatmentTraceMapper.getWorkloadStatisticsQualityControl(params);
		Map<String,Object> targetMap = null;
		for (Map<String, Object> map2 : list) {
			targetMap = new LinkedHashMap<>();
			targetMap.put("userName", map2.get("userName")==null?"":(String)map2.get("userName"));
			targetMap.put("perosnalSum", map2.get("perosnalSum")==null?0:(long)map2.get("perosnalSum"));
			String rate="0";
			if( map2.get("personalPercent")!=null){
				BigDecimal rateDecimal =(BigDecimal) map2.get("personalPercent");
				rate=rateDecimal.toString();
			}
			targetMap.put("personalPercent", rate);
			targetList.add(targetMap);
		}
		
		return targetList;
		
	}

	@Transactional(readOnly = true)
	@TargetDataSource(name = "ds1")
	@Override
	public List<Map<String, Object>> paginationWorkExcelList(String beginDate, String endDate) {
		Map<String,Object> params = new HashMap<>();
		params.put("beginDate", beginDate);
		params.put("endDate", endDate);
		List<Map<String,Object>> targetList = new ArrayList<>();
		List<Map<String,Object>> list =  treatmentTraceMapper.getWorkloadStatisticsPagenation(params);
		Map<String,Object> targetMap = null;
		for (Map<String, Object> map2 : list) {
			targetMap = new LinkedHashMap<>();
			targetMap.put("userName", map2.get("userName")==null?"":(String)map2.get("userName"));
			targetMap.put("perosnalSum", map2.get("perosnalSum")==null?0:(long)map2.get("perosnalSum"));
			String rate="0";
			if( map2.get("personalPercent")!=null){
				BigDecimal rateDecimal =(BigDecimal) map2.get("personalPercent");
				rate=rateDecimal.toString();
			}
			targetMap.put("personalPercent", rate);
			targetList.add(targetMap);
		}
		
		return targetList;
	}

	@Transactional(readOnly = true)
	@TargetDataSource(name = "ds1")
	@Override
	public List<Map<String, Object>> catalogeExcelList(String beginDate, String endDate) {
		Map<String,Object> params = new HashMap<>();
		params.put("beginDate", beginDate);
		params.put("endDate", endDate);
		List<Map<String,Object>> targetList = new ArrayList<>();
		List<Map<String,Object>> list =  treatmentTraceMapper.getWorkloadStatisticsCatalog(params);
		Map<String,Object> targetMap = null;
		for (Map<String, Object> map2 : list) {
			targetMap = new LinkedHashMap<>();
			targetMap.put("userName", map2.get("userName")==null?"":(String)map2.get("userName"));
			targetMap.put("perosnalSum", map2.get("perosnalSum")==null?0:(long)map2.get("perosnalSum"));
			String rate="0";
			if( map2.get("personalPercent")!=null){
				BigDecimal rateDecimal =(BigDecimal) map2.get("personalPercent");
				rate=rateDecimal.toString();
			}
			targetMap.put("personalPercent", rate);
			targetList.add(targetMap);
		}
		
		return targetList;
	}
	
	
	
	

}
